package com.nulldev.util.web.HttpServer3.extensions.nCaptcha.captchas.obftext;

import java.awt.image.BufferedImage;

import com.nulldev.util.VariableAPI.RandomUtil;
import com.nulldev.util.data.Images.ImageUtil;
import com.nulldev.util.data.Images.legacy.TextObf;
import com.nulldev.util.data.Images.legacy.TextObf.ObfuscationMethod;
import com.nulldev.util.data.Sets.DataSet;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.Captcha;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3.Request;

public class TrueFalseStatement implements Captcha {

	private static final String[] TRUE_STATEMENT_VALUES = new String[]
		{ "true", "yes", "TRUE", "YES" };
	private static final String[] FALSE_STATEMENT_VALUES = new String[]
		{ "false", "FALSE", "no", "NO" };

	@Override
	public DataSet<String, String[]> generateBase64Image(final int botLevel, final Request request) {
		final int i0 = RandomUtil.randInt(-200, 200);
		final int i1 = RandomUtil.randInt(-200, 200);
		final boolean x = RandomUtil.randomBoolean();
		final boolean expected = (x ? i0 > i1 : i1 < i0);
		final BufferedImage obf = TextObf.obfuscate((x ? i0 + " MORE than " + i1 : i1 + " less THAN " + i0),
				(botLevel > 1 ? ObfuscationMethod.Noise : ObfuscationMethod.RainbowBackgroundNoise /* PixelObfuscation */),
				TextObf.__font(BASE_TEXT_SIZE));
		final String base64 = ImageUtil.encodeToString(obf, "png");
		return new DataSet<String, String[]>("data:image/png;base64," + base64, (expected ? TRUE_STATEMENT_VALUES : FALSE_STATEMENT_VALUES));
	}

	@Override
	public String getType() {
		return "truefalse";
	}
}
